##  基础函数库
import numpy as np
import pandas as pd
from sklearn import metrics
## 为了正确评估模型性能，将数据划分为训练集和测试集，并在训练集上训练模型，在测试集上验证模型性能。
from sklearn.model_selection import train_test_split
## 导入XGBoost模型
from xgboost.sklearn import XGBClassifier

df = pd.read_csv('../../doc/promotion/aicampml/happiness_train_complete.csv',encoding="unicode_escape")
df.fillna(-1)
y = df.happiness
x = df.drop(["id","happiness","survey_time","edu_other","property_other","invest_other"],axis=1)
## 为了正确评估模型性能，将数据划分为训练集和测试集，并在训练集上训练模型，在测试集上验证模型性能。
data_target_part = y
data_features_part = x
## 测试集大小为20%， 80%/20%分
x_train, x_test, y_train, y_test = train_test_split(data_features_part, data_target_part, test_size = 0.2)

# ## 从sklearn库中导入网格调参函数
# from sklearn.model_selection import GridSearchCV
# ## 定义参数取值范围
# learning_rate = [0.1, 0.3, 0.6]
# subsample = [0.8, 0.9]
# colsample_bytree = [0.6, 0.8]
# max_depth = [3,5,8]
# parameters = { 'learning_rate': learning_rate,
#               'subsample': subsample,
#               'colsample_bytree':colsample_bytree,
#               'max_depth': max_depth}
# model = XGBClassifier(n_estimators = 50)
# ## 进行网格搜索
# clf = GridSearchCV(model, parameters, cv=3, scoring='accuracy',verbose=1,n_jobs=-1)
# clf = clf.fit(x_train, y_train)
# print(clf.best_params_)

## 定义 XGBoost模型
clf = XGBClassifier(colsample_bytree = 0.6, learning_rate = 0.1, max_depth= 5, subsample = 0.9)
# 在训练集上训练XGBoost模型
clf.fit(x_train, y_train)
## 在训练集和测试集上分布利用训练好的模型进行预测
train_predict = clf.predict(x_train)
test_predict = clf.predict(x_test)
print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_train,train_predict))
print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_test,test_predict))


# dfTest = pd.read_csv('../../doc/promotion/aicampml/happiness_test_complete.csv',encoding="unicode_escape")
# dfTest.fillna(-1)
# testData = dfTest.drop(["survey_time","edu_other","property_other","invest_other"],axis=1)
# test_result = clf.predict(testData)
# #
# dataframe = pd.DataFrame({"id":testData["id"].array,"happiness":test_result})
# dataframe.to_csv("../../doc/promotion/aicampml/result.csv",index=False,sep=',')

if __name__ == '__main__':
    pass